package com.oazon.moco.lan.core.client.impl;

import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.oazon.common.util.RequestManager;
import com.oazon.moco.lan.core.client.ILanRequest;
import com.oazon.moco.lan.core.http.bean.JsonResponse;
import com.oazon.moco.link.core.LinkSDK;

public class LanRequest implements ILanRequest {

	private static final String TAG = "LanRequest";

	private RequestQueue queue;

	private InnerListener innerListener = null;
	
	private Context context;

	public LanRequest(Context context) {
		this.context = context;
		queue = RequestManager.getRequestQueue();
	}

	@Override
	public void postRequest(String url, final Map<String, String> parems,
			InnerListener innerListener) {
		this.innerListener = innerListener;
		Request<String> request = new StringRequest(Request.Method.POST, url,
				listener, errorListener) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				return parems;
			}
		};
		if(LinkSDK.DEBUG_LOCK){
			Log.d(TAG, "url:" + url);
			Intent intent = new Intent();
			intent.setAction("com.moco.debug");
			intent.putExtra("url", url);
			LinkSDK.getAppContext().sendBroadcast(intent);
		}
		request.setShouldCache(true);
		//去掉重发机制
		request.setRetryPolicy(new DefaultRetryPolicy(0, 0, 0));
		queue.add(request);
	}

	@Override
	public void addRequest(String url, InnerListener innerListener) {
		this.innerListener = innerListener;
		Request<String> request = new StringRequest(url, listener,
				errorListener);
		request.setShouldCache(true);
		request.setRetryPolicy(new DefaultRetryPolicy(0, 0, 0));
		queue.add(request);
	}

	private Response.Listener<String> listener = new Response.Listener<String>() {

		@Override
		public void onResponse(String response) {
			try {
				if (response != null) {

					JsonResponse jsonResponse = JSON.parseObject(response,
							JsonResponse.class);
					if (innerListener != null) {
						innerListener.onResponse(jsonResponse);
					}
				}
			} catch (Exception e) {
				if(LinkSDK.DEBUG_LOCK){
					Intent intent = new Intent("com.moco.debug");
					intent.putExtra("response", response);
					context.sendBroadcast(intent);
				}
			}
		}
	};

	private Response.ErrorListener errorListener = new Response.ErrorListener() {

		@Override
		public void onErrorResponse(VolleyError error) {
			try {
				if (innerListener != null) {
					innerListener.onErrorResponse(error.getCause().toString());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	};

	public interface InnerListener {
		void onResponse(JsonResponse jsonResponse);

		void onErrorResponse(String error);
	}
}
